<template>
  <div class="my-container">

    <div v-if="user" class="header user-info">
      <!-- 基本信息 -->
      <div class="base-info">
        <div class="left">
          <van-image
            fit="cover"
            :src="userInfo.photo"
            class="avatar"
            round
          />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <van-grid :column-num="2" class="grid-nav">
      <van-grid-item class="grid-item">
        <i slots="icon" class="font_family icon-shoucang"></i>
        <span class="text" slots="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slots="icon" class="font_family icon-lishi"></i>
        <span class="text" slots="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell @click="onLogout" v-if="user" class="logout-cell" title="退出登录" center clickable/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'

export default {
  name: 'MyIndex',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  mounted () {},
  methods: {
    onLogout () {
      this.$dialog.confirm({
        title: '确认退出吗'
        // message: '弹窗内容'
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
        // console.log(this.userInfo)
      } catch (err) {
        this.$toast('获取数据失败，请稍后再试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .header {
    height: 190px;
    background: url("~@/assets/banner.png");
    //background-color: red;
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 71px;
        height: 71px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 16px;
        color: #fff;
      }
    }
  }
  .user-info {
    //background: red;
    .base-info {
      height: 131px;
      padding: 38px 20px 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 65px;
          height: 65px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 16px;
          color: #fff;
          margin-left: 15px;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        flex: 1;
        height: 65px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 18px;
        }
        .text {
          font-size: 12px;
        }
      }
    }
  }
  .grid-nav{
    .grid-item{
      height: 70px;
      i.font_family {
        font-size: 23px;
      }
      .icon-shoucang {
        color: #EB5253;
      }
      .icon-lishi {
        color: #FF9D1D;
      }
      .text{
        color: #000;
        margin-top: 6px;
        font-size: 14px;
      }
    }
  }
  .logout-cell {
    height: 50px;
    text-align: center;
    color: #EB5253;
    margin: 5px 0;
    font-size: 16px;
  }
}
</style>
